using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.ComponentModel;

namespace Facebook.Web.Fbml
{
    [Designer(typeof(Design.EditorButtonDesigner))]
    public class EditorButton : Control
    {
        private string value;
        public event EventHandler Click;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (!DesignMode)
            {
                Page.LoadComplete += new EventHandler(Page_LoadComplete);
            }
        }

        public string Value
        {
            get { return value; }
            set { this.value = value; }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("<fb:editor-button");
            writer.WriteAttribute("name", ClientID);
            writer.WriteAttribute("value", Value);
            writer.Write("/>");
        }
        
        void Page_LoadComplete(object sender, EventArgs e)
        {
            if (Page.Request[ClientID] != null)
            {
                OnClick(EventArgs.Empty);
            }
        }

        protected virtual void OnClick(EventArgs e)
        {
            if (Click != null)
            {
                Click(this, e);
            }
        }
    }
}
